﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;

namespace NotebookS2
{
    public partial class FND_BP : RibbonForm
    {
        public FND_BP()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }

        public int SearchIndex { get; set; }

        
        public string CardCode { get; set; }
        public string CardName { get; set; }
        public string BusinessPartnerType { get; set; }
        public string Address { get; set; }
        public string UnitNo { get; set; }
        public string Street { get; set; }
        public string Baranggay { get; set; }
        public string CityOrMunicipality { get; set; }
        public string PostalCode { get; set; }
        public string ProvinceOrMetro { get; set; }

        public string Fax { get; set; }
        public string Phone { get; set; }
        public string ContactPerson { get; set; }
        public string TIN { get; set; }
        public int PaymentTerm { get; set; }
        public double CreditLimit { get; set; }
        public string PriceList { get; set; }
        public string BuyerSalesMan { get; set; }
        public string AreaCode { get; set; }
        public string Teritorry { get; set; }
        public string CustomerGroup { get; set; }
        public string ParentCode { get; set; }
        public string Institution { get; set; }
        public string StoreCode { get; set; }
        public string VATCode { get; set; }
        public double WithHoldingTax { get; set; }
        public string Division { get; set; }

        public string Discount1 { get; set; }
        public string Discount2 { get; set; }
        public string Discount3 { get; set; }
        public string Discount4 { get; set; }
        public string Document { get; set; }

        List<AddressBookUnit> oBPUnits = new List<AddressBookUnit>();

        public void SetSearchByBPCode(string _SEARCH_TEXT)
        {
            cboSearch.SelectedIndex = 0;
            this.Text = this.Text + " - " + this.BusinessPartnerType;
            txtSearch.Text = _SEARCH_TEXT;
            btnSearch_Click(null, null);
        }

        public void SetSearchByStoreCode(string _SEARCH_TEXT)
        {
            cboSearch.SelectedIndex = 2;
            this.Text = this.Text + " - " + this.BusinessPartnerType;
            txtSearch.Text = _SEARCH_TEXT;
            btnSearch_Click(null, null);
        }

        void LoadByCode(string sCode)
        {
            try
            {
                DataTable dt = new DataTable();
                AddressBookManager oBPManager = new AddressBookManager();
                AddressBookUnit oBPUnit = new AddressBookUnit();

                oBPManager.ConnectionString = this.ConnectionString;
                oBPManager.Open();

                dt = oBPManager.BrowseByBPCode(sCode, this.BusinessPartnerType);
                oBPManager.Close();


                gridControl1.DataSource = dt;
                gridControl1.Refresh();

                //oBPUnits = new List<AddressBookUnit>();
                //foreach (DataRow row in dt.Rows)
                //{
                //    oBPUnit = new AddressBookUnit();

                //    oBPUnit.StoreCode = row["StoreCode"].ToString();
                //    oBPUnit.Code = row["BPCode"].ToString();
                //    oBPUnit.Name = row["BPName"].ToString();
                //    oBPUnit.Address = row["Address"].ToString();
                //    oBPUnit.UnitNo = row["UnitNo"].ToString();
                //    oBPUnit.Street = row["Street"].ToString();
                //    oBPUnit.Barangay = row["Brgy"].ToString();
                //    oBPUnit.CityOrMunicipality = row["CityOrMunicipal"].ToString();
                //    oBPUnit.PostalCode = row["postalCode"].ToString();
                //    oBPUnit.ProvinceOrMetro = row["ProvinceOrMetro"].ToString();
                //    oBPUnit.ContactPerson = row["ContactPerson"].ToString();
                //    oBPUnit.TIN = row["TIN"].ToString();
                //    oBPUnit.Phone = row["PHONE"].ToString();
                //    oBPUnit.PaymentTerm = Convert.ToInt16(row["PaymentTerm"]);
                //    oBPUnit.CreditLimit = Convert.ToDouble(row["CreditLimit"]);
                //    oBPUnit.PriceList = row["PriceList"].ToString();
                //    oBPUnit.BuyerSalesMan = row["SMCODE"].ToString();

                //    oBPUnit.AreaCode = row["AreaCode"].ToString();
                //    oBPUnit.Teritorry = row["Teritorry"].ToString();
                //    oBPUnit.CustomerGroup = row["CustomerGroup"].ToString();
                //    oBPUnit.ParentCode = row["BPCodeParent"].ToString();
                //    oBPUnit.Institution = row["Institution"].ToString();
                //    oBPUnit.StoreCode = row["StoreCode"].ToString();
                //    oBPUnit.VATCode = row["VATCode"].ToString();
                //    oBPUnit.WithHoldingTax = Convert.ToDouble(row["WithHoldingTax"].ToString());
                //    if (row["Division"] != DBNull.Value)
                //    {
                //        oBPUnit.Division = row["Division"].ToString();
                //    }
                //    else
                //    {
                //        oBPUnit.Division = "";
                //    }
                //    if (row["StoreCode"] != DBNull.Value)
                //    {
                //        oBPUnit.StoreCode = row["StoreCode"].ToString();
                //    }
                //    else
                //    {
                //        oBPUnit.StoreCode = "";
                //    }


                //    if (row["Disc1"] != DBNull.Value)
                //    {
                //        oBPUnit.Discount1 = row["Disc1"].ToString();
                //    }
                //    else
                //    {
                //        oBPUnit.Discount1 = "";
                //    }

                //    if (row["Disc2"] != DBNull.Value)
                //    {
                //        oBPUnit.Discount2 = row["Disc2"].ToString();
                //    }
                //    else
                //    {
                //        oBPUnit.Discount2 = "";
                //    }

                //    if (row["Disc3"] != DBNull.Value)
                //    {
                //        oBPUnit.Discount3 = row["Disc3"].ToString();
                //    }
                //    else
                //    {
                //        oBPUnit.Discount3 = "";
                //    }

                //    if (row["Disc4"] != DBNull.Value)
                //    {
                //        oBPUnit.Discount4 = row["Disc4"].ToString();
                //    }
                //    else
                //    {
                //        oBPUnit.Discount4 = "";
                //    }
                //    if (row["Documnt"] != DBNull.Value)
                //    {
                //        oBPUnit.Document = row["Documnt"].ToString();
                //    }
                //    else
                //    {
                //        oBPUnit.Document = "";
                //    }
                //    oBPUnits.Add(oBPUnit);

                //}
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }

        void LoadByName(string sName)
        {
            try
            {
                DataTable dt = new DataTable();
                AddressBookManager oBPManager = new AddressBookManager();
                AddressBookUnit oBPUnit = new AddressBookUnit();

                oBPManager.ConnectionString = this.ConnectionString;
                oBPManager.Open();

                dt = oBPManager.BrowseByBPName(sName, this.BusinessPartnerType);
                oBPManager.Close();


                gridControl1.DataSource = dt;
                gridControl1.Refresh();

                //oBPUnits = new List<AddressBookUnit>();


                //foreach (DataRow row in dt.Rows)
                //{
                //    oBPUnit = new AddressBookUnit();
                //    oBPUnit.StoreCode = row["StoreCode"].ToString();
                //    oBPUnit.Code = row["BPCode"].ToString();
                //    oBPUnit.Name = row["BPName"].ToString();
                //    oBPUnit.Address = row["Address"].ToString();
                //    oBPUnit.UnitNo = row["UnitNo"].ToString();
                //    oBPUnit.Street = row["Street"].ToString();
                //    oBPUnit.Barangay = row["Brgy"].ToString();
                //    oBPUnit.CityOrMunicipality = row["CityOrMunicipal"].ToString();
                //    oBPUnit.PostalCode = row["postalCode"].ToString();
                //    oBPUnit.ProvinceOrMetro = row["ProvinceOrMetro"].ToString();

                //    oBPUnit.Phone = row["Phone"].ToString();
                //    oBPUnit.Fax = row["Fax"].ToString();
                //    oBPUnit.ContactPerson = row["ContactPerson"].ToString();
                //    oBPUnit.TIN = row["TIN"].ToString();
                //    oBPUnit.PaymentTerm = Convert.ToInt16(row["PaymentTerm"]);
                //    oBPUnit.CreditLimit = Convert.ToDouble(row["CreditLimit"]);
                //    oBPUnit.PriceList = row["PriceList"].ToString();
                //    oBPUnit.BuyerSalesMan = row["SMCODE"].ToString();
                //    oBPUnit.AreaCode = row["AreaCode"].ToString();
                //    oBPUnit.Teritorry = row["Teritorry"].ToString();
                //    oBPUnit.CustomerGroup = row["CustomerGroup"].ToString();
                //    oBPUnit.ParentCode = row["BPCodeParent"].ToString();
                //    oBPUnit.Institution = row["Institution"].ToString();

                //    oBPUnit.Division = row["Division"].ToString();
                //    oBPUnit.StoreCode = row["StoreCode"].ToString();

                //    oBPUnit.VATCode = row["VATCode"].ToString();
                //    if (row["Disc1"] != DBNull.Value)
                //    {
                //        oBPUnit.Discount1 = row["Disc1"].ToString();
                //    }
                //    else
                //    {
                //        oBPUnit.Discount1 = "";
                //    }

                //    if (row["Disc2"] != DBNull.Value)
                //    {
                //        oBPUnit.Discount2 = row["Disc2"].ToString();
                //    }
                //    else
                //    {
                //        oBPUnit.Discount2 = "";
                //    }

                //    if (row["Disc3"] != DBNull.Value)
                //    {
                //        oBPUnit.Discount3 = row["Disc3"].ToString();
                //    }
                //    else
                //    {
                //        oBPUnit.Discount3 = "";
                //    }

                //    if (row["Disc4"] != DBNull.Value)
                //    {
                //        oBPUnit.Discount4 = row["Disc4"].ToString();
                //    }
                //    else
                //    {
                //        oBPUnit.Discount4 = "";
                //    }
                //    if (row["Documnt"] != DBNull.Value)
                //    {
                //        oBPUnit.Document = row["Documnt"].ToString();
                //    }
                //    else
                //    {
                //        oBPUnit.Document = "";
                //    }
                //    oBPUnits.Add(oBPUnit);

                    
                //}

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        void LoadByStoreCode(string sStoreCode)
        {
            try
            {
                DataTable dt = new DataTable();
                AddressBookManager oBPManager = new AddressBookManager();
                AddressBookUnit oBPUnit = new AddressBookUnit();

                oBPManager.ConnectionString = this.ConnectionString;
                oBPManager.Open();

                dt = oBPManager.BrowseByStoreCode(sStoreCode, this.BusinessPartnerType);
                oBPManager.Close();


                gridControl1.DataSource = dt;
                gridControl1.Refresh();

                //oBPUnits = new List<AddressBookUnit>();


                //foreach (DataRow row in dt.Rows)
                //{
                   

                //    oBPUnit = new AddressBookUnit();
                //    oBPUnit.StoreCode = row["StoreCode"].ToString();
                //    oBPUnit.Code = row["BPCode"].ToString();
                //    oBPUnit.Name = row["BPName"].ToString();
                //    oBPUnit.Address = row["Address"].ToString();
                //    oBPUnit.UnitNo = row["UnitNo"].ToString();
                //    oBPUnit.Street = row["Street"].ToString();
                //    oBPUnit.Barangay = row["Brgy"].ToString();
                //    oBPUnit.CityOrMunicipality = row["CityOrMunicipal"].ToString();
                //    oBPUnit.PostalCode = row["postalCode"].ToString();
                //    oBPUnit.ProvinceOrMetro = row["ProvinceOrMetro"].ToString();

                //    oBPUnit.Phone = row["Phone"].ToString();
                //    oBPUnit.Fax = row["Fax"].ToString();
                //    oBPUnit.ContactPerson = row["ContactPerson"].ToString();
                //    oBPUnit.TIN = row["TIN"].ToString();
                //    oBPUnit.PaymentTerm = Convert.ToInt16(row["PaymentTerm"]);
                //    oBPUnit.CreditLimit = Convert.ToDouble(row["CreditLimit"]);
                //    oBPUnit.PriceList = row["PriceList"].ToString();
                //    oBPUnit.BuyerSalesMan = row["SMCODE"].ToString();
                //    oBPUnit.AreaCode = row["AreaCode"].ToString();
                //    oBPUnit.Teritorry = row["Teritorry"].ToString();
                //    oBPUnit.CustomerGroup = row["CustomerGroup"].ToString();
                //    oBPUnit.ParentCode = row["BPCodeParent"].ToString();
                //    oBPUnit.Institution = row["Institution"].ToString();

                //    oBPUnit.Division = row["Division"].ToString();
                //    oBPUnit.StoreCode = row["StoreCode"].ToString();

                //    oBPUnit.VATCode = row["VATCode"].ToString();
                //    if (row["Disc1"] != DBNull.Value)
                //    {
                //        oBPUnit.Discount1 = row["Disc1"].ToString();
                //    }
                //    else
                //    {
                //        oBPUnit.Discount1 = "";
                //    }

                //    if (row["Disc2"] != DBNull.Value)
                //    {
                //        oBPUnit.Discount2 = row["Disc2"].ToString();
                //    }
                //    else
                //    {
                //        oBPUnit.Discount2 = "";
                //    }

                //    if (row["Disc3"] != DBNull.Value)
                //    {
                //        oBPUnit.Discount3 = row["Disc3"].ToString();
                //    }
                //    else
                //    {
                //        oBPUnit.Discount3 = "";
                //    }

                //    if (row["Disc4"] != DBNull.Value)
                //    {
                //        oBPUnit.Discount4 = row["Disc4"].ToString();
                //    }
                //    else
                //    {
                //        oBPUnit.Discount4 = "";
                //    }
                //    if (row["Documnt"] != DBNull.Value)
                //    {
                //        oBPUnit.Document = row["Documnt"].ToString();
                //    }
                //    else
                //    {
                //        oBPUnit.Document = "";
                //    }
                //    oBPUnits.Add(oBPUnit);

                   
                //}

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        private void FND_BUSINESSPARTNER_Load(object sender, EventArgs e)
        {
            
            if (txtSearch.Text == "")
            {
                cboSearch.SelectedIndex = 1;
                
                this.Text = this.Text + " - " + this.BusinessPartnerType;
                txtSearch.Focus();

            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string sSearch = txtSearch.Text + '%';
            try
            {
                if (cboSearch.SelectedIndex == 0)
                {
                    LoadByCode(sSearch);
                }
                else if (cboSearch.SelectedIndex ==1)
                {
                    LoadByName(sSearch);
                }
                else if (cboSearch.SelectedIndex == 2)
                {
                    LoadByStoreCode(sSearch);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            int iFocusedRowIndex = gridView1.FocusedRowHandle;

            DataTable dt = new DataTable();
            AddressBookManager oManager = new AddressBookManager();


            this.StoreCode = gridView1.GetRowCellValue(iFocusedRowIndex, gridView1.Columns["StoreCode"]).ToString();
            this.CardCode = gridView1.GetRowCellValue(iFocusedRowIndex, gridView1.Columns["BPCode"]).ToString();
            this.CardName = gridView1.GetRowCellValue(iFocusedRowIndex, gridView1.Columns["BPName"]).ToString();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            dt = oManager.GetByBPCode(this.CardCode, this.BusinessPartnerType);
            
            oManager.Close();

            DataRow oRow = dt.Rows[0];


            this.UnitNo = oRow["UnitNo"].ToString();
            this.Address = oRow["Address"].ToString();
            this.Street = oRow["Street"].ToString();
            this.Baranggay = oRow["Brgy"].ToString();
            this.CityOrMunicipality = oRow["CityOrMunicipal"].ToString();
            this.PostalCode = oRow["PostalCode"].ToString();
            this.ProvinceOrMetro = oRow["ProvinceOrMetro"].ToString();
            this.ContactPerson = oRow["ContactPerson"].ToString();
            this.Fax = oRow["Fax"].ToString();
            this.Phone = oRow["Phone"].ToString();
            this.TIN = oRow["TIN"].ToString();
            this.PaymentTerm = Convert.ToInt16(oRow["PaymentTerm"].ToString());
            this.CreditLimit = Convert.ToDouble(oRow["CreditLimit"]);
            this.PriceList = oRow["PriceList"].ToString();
            this.BuyerSalesMan = oRow["SMCODE"].ToString();

            this.AreaCode = oRow["AreaCode"].ToString();
            this.Teritorry = oRow["Teritorry"].ToString();
            this.CustomerGroup = oRow["CustomerGroup"].ToString();
            this.ParentCode = oRow["BPCodeParent"].ToString();
            this.Institution = oRow["Institution"].ToString();
            this.VATCode = oRow["VATCode"].ToString();
            this.WithHoldingTax = Convert.ToDouble(oRow["WithHoldingTax"].ToString());
            this.Division = oRow["Division"].ToString();
            this.StoreCode = oRow["StoreCode"].ToString();

            this.Discount1 = oRow["Disc1"].ToString();
            this.Discount2 = oRow["Disc2"].ToString();
            this.Discount3 = oRow["Disc3"].ToString();
            this.Discount4 = oRow["Disc4"].ToString();
            this.Document = oRow["Documnt"].ToString();
             
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void lvwSearched_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }

            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
                this.Dispose();
            }
        }

        private void lvwSearched_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(sender, e);
                if (gridView1.RowCount > 0)
                {
                    gridControl1.Focus();
                }
            }
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void FND_BP_Shown(object sender, EventArgs e)
        {
            cboSearch.SelectedIndex = this.SearchIndex;
        }
    }
}
